﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlServerCe;

namespace ProjectoPV_ESW.Account.Ementa
{
    /// <summary>
    /// Classe desenvolvida por Sofia Pombo.
    /// </summary>
    public partial class CriarListaPrato : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                EmentaId = System.Convert.ToInt32((string)Session["IdEmentaBase"]);
                Edit = (bool)Session["EditEmenta"];
            }
            catch (Exception)
            {
                Response.Redirect("CriarEmentaDia.aspx");
            }
            
        }

        public int EmentaId { get; set; }
        
        protected void Button1_Click(object sender, EventArgs e)
        {
            //Se for para editar uma lista, primeiro têm que se apagar os valores que estão na BD, para não haver uma ementa com mais de 5 pratos
            string connectionStringJardim = ConfigurationManager.ConnectionStrings["JardimEntitiesString"].ConnectionString;
            SqlCeConnection conn = new SqlCeConnection(connectionStringJardim);

            if (Edit)
            {
                string deleteSqlCommand = "DELETE FROM ListaPratos WHERE Ementa_EmentaId =@ementaId";
                SqlCeCommand deleteCommand = new SqlCeCommand(deleteSqlCommand, conn);
                deleteCommand.Parameters.AddWithValue("@ementaId",EmentaId);
                try 
	            {	        
                    conn.Open();
                    LabelErr.Text=deleteCommand.ExecuteNonQuery().ToString();
	            }
	            catch (Exception exc)
	            {
		            LabelErr.Text = exc.Message + " - " + exc.Source;
	            }
                finally
                {
                    conn.Close();
                }

            }
            try{
                Prato1Id=System.Convert.ToInt32(DropDownList2.SelectedItem.Value);
                Prato2Id=System.Convert.ToInt32(DropDownList3.SelectedItem.Value);
                Prato3Id=System.Convert.ToInt32(DropDownList4.SelectedItem.Value);
                Prato4Id=System.Convert.ToInt32(DropDownList5.SelectedItem.Value);
                Prato5Id=System.Convert.ToInt32(DropDownList6.SelectedItem.Value);
            }catch(Exception){
                LabelErr.Text = "Erro - Não seleccionou todos os campos";
                           
            }
            if (Prato1Id == Prato2Id || Prato1Id == Prato3Id || Prato1Id == Prato4Id || Prato1Id == Prato5Id
                || Prato2Id == Prato3Id || Prato2Id == Prato4Id || Prato2Id == Prato5Id
                || Prato3Id == Prato4Id || Prato3Id == Prato5Id
                || Prato4Id == Prato5Id)
            {
                LabelErr.Text = "Erro - Possui pratos iguais na sua selecção.";
            }
            else
            {


                //inserção!!
                string commandText = @"INSERT INTO ListaPratos(Ementa_EmentaId, Prato_PratoId) VALUES (@idementa, @idprato)";

                try
                {
                    conn.Open();
                    SqlCeCommand insertCommand = new SqlCeCommand(commandText, conn);
                    insertCommand.Parameters.AddWithValue("@idementa", EmentaId);
                    insertCommand.Parameters.AddWithValue("@idprato", Prato1Id);
                    insertCommand.ExecuteNonQuery();
                    insertCommand.Parameters.Clear();
                    insertCommand.Parameters.AddWithValue("@idementa", EmentaId);
                    insertCommand.Parameters.AddWithValue("@idprato", Prato2Id);
                    insertCommand.ExecuteNonQuery();
                    insertCommand.Parameters.Clear();
                    insertCommand.Parameters.AddWithValue("@idementa", EmentaId);
                    insertCommand.Parameters.AddWithValue("@idprato", Prato3Id);
                    insertCommand.ExecuteNonQuery();
                    insertCommand.Parameters.Clear();
                    insertCommand.Parameters.AddWithValue("@idementa", EmentaId);
                    insertCommand.Parameters.AddWithValue("@idprato", Prato4Id);
                    insertCommand.ExecuteNonQuery();
                    insertCommand.Parameters.Clear();
                    insertCommand.Parameters.AddWithValue("@idementa", EmentaId);
                    insertCommand.Parameters.AddWithValue("@idprato", Prato5Id);
                    insertCommand.ExecuteNonQuery();
                    this.LabelOK.Text = "Inserção feita!";
                }
                catch (Exception ex)
                {
                    LabelErr.Text = ex.Message + " - " + ex.Source;
                }
                finally
                {
                    conn.Close();
                    Response.Redirect("CriarEmentaDia.aspx");
                }
            }
        }

        public int Prato1Id { get; set; }
        public int Prato2Id { get; set; }
        public int Prato3Id { get; set; }
        public int Prato4Id { get; set; }
        public int Prato5Id { get; set; }
        public bool Edit { get; set; }


    }
}